﻿// JScript File for managing view mode for maxblox pages by sanal, live on Feb 10, 2010
//$('textarea').autogrow();
//handleViewMode();
function handleViewMode()
{
var vm_loc = document.location.href;
//vm_loc = "vmode=0";
if (vm_loc.toLowerCase().indexOf("vmode=1") > -1) {
    //var str = '';
    var remElements = "";
    var elem = document.getElementById('aspnetForm').elements;
    var isBrwsrIE = isIEBrowser();
    for (var i = 0; i < elem.length; i++) {
        if (elem[i].disabled != true) 
            continue;
        if (elem[i].type == "hidden") 
            continue;
        if (elem[i].type == "radio") 
            continue;
        if (elem[i].type == "button") 
            continue;
        var originalValue = "";
        var elementWidth;
        var elementHeight;
        if (elem[i].type == "select-one") {
            if (elem[i].selectedIndex > -1)
                originalValue = $('<div/>').text(elem[i].options[elem[i].selectedIndex].text).html();
            if (originalValue == "- Select One -") 
                originalValue = "";
            if (originalValue == "") 
                originalValue = "&nbsp;";
            elementWidth = getWidthOverrided(elem[i]);
            elementHeight = getHeightOverrided(elem[i]);
            //elem[i].parentNode.replaceChild(createNewElement(originalValue,elem[i].id,elementWidth,elementHeight,isBrwsrIE),elem[i]); 
            elem[i].parentNode.insertBefore(createNewElement(originalValue, elem[i].id, elementWidth, elementHeight, isBrwsrIE,false), elem[i]);
        }
        else 
            if (elem[i].type == "select-multiple") {
                originalValue = getSelectedOptions((elem[i]));
                if (originalValue == "") 
                    originalValue = "&nbsp;";
                elementWidth = getWidthOverrided(elem[i]);
                elementHeight = getHeightOverrided(elem[i]);
                elem[i].parentNode.insertBefore(createNewElement(originalValue, elem[i].id, elementWidth, elementHeight, isBrwsrIE,false), elem[i]);
            }
            else 
                if (elem[i].type == "checkbox") {
                    originalValue = elem[i].checked == true ? "Yes" : "No";
                    originalValue = "<img src='Images/" + (originalValue == "Yes" ? "checked_disabled.gif" : "unchecked_disabled.gif") + "' alt='" + originalValue + "' title='" + originalValue + "' border='0' />";
                    elem[i].parentNode.insertBefore(createNewElement(originalValue, elem[i].id, 0, 0, isBrwsrIE,false), elem[i]);
                } 
           else if (elem[i].type == "textarea"){
                    originalValue = $('<div/>').text(elem[i].value).html();
                    if (originalValue == "") 
                        originalValue = "&nbsp;";   
					originalValue = originalValue.replace(/\n/g,"<br>").replace(/\r/g,"");                 
                    elementWidth = getWidthOverrided(elem[i]);
                    elementHeight = getHeightOverrided(elem[i]);
                    elem[i].parentNode.insertBefore(createNewElement(originalValue, elem[i].id, elementWidth, elementHeight, isBrwsrIE,true), elem[i]);
                }               
                else {
                    originalValue = $('<div/>').text(elem[i].value).html();
                    if (originalValue == "") 
                        originalValue = "&nbsp;";                    
                    elementWidth = getWidthOverrided(elem[i]);
                    elementHeight = getHeightOverrided(elem[i]);
                    elem[i].parentNode.insertBefore(createNewElement(originalValue, elem[i].id, elementWidth, elementHeight, isBrwsrIE,false), elem[i]);
                }
        /*
         str += "<b>Type:</b>" + elem[i].type + "&nbsp&nbsp";
         str += "<b>Name:</b>" + elem[i].name + "&nbsp;&nbsp;";
         str += "<b>Value:</b><i>" + elem[i].value + "</i>&nbsp;&nbsp;";
         str += "<BR>";
         */
        remElements += elem[i].id + "|rem|";
        elem[i].parentNode.style.cssText = "white-space:nowrap;";
    }
    if (vm_loc.toLowerCase().indexOf("transdoc.aspx") > -1) // Added by srivatsan for Total field update
    {
        transdoc_refreshTotalFieldsWithArg(true);
    }
    removeLements(remElements);
}
}
function getWidthOverrided(element){
    var wid = getWidth(element);
    if (wid == "undefined" || wid == null || wid == "") {
        return 200;
    }
    else 
        return wid - 1;
}

function getWidth(element){

    if (typeof element.clip !== "undefined") //netscape
    {
        return element.clip.width;
    }
    else {
        if (element.style.pixelWidth)//opera 
        {
            return element.style.pixelWidth;
        }
        else //IE and firefox
        {
            return element.offsetWidth;
        }
    }
}

function getHeightOverrided(element){
    var wid = getHeight(element);
    if (wid == "undefined" || wid == null || wid == "") {
        return 20;
    }
    else 
        return wid - 1;
}

function getHeight(element){

    if (typeof element.clip !== "undefined") //netscape
    {
        return element.clip.height;
    }
    else {
        if (element.style.pixelHeight)//opera 
        {
            return element.style.pixelHeight;
        }
        else //IE and firefox
        {
            return element.offsetHeight;
        }
    }
}

function getSelectedOptions(oList){
    var sdValues = [];//modified by Sampath on 04-Apr-2011 1 - 0, for starting from 0 element
    for (var i = 0; i < oList.options.length; i++) {
        if (oList.options[i].selected == true) {
            sdValues.push($('<div/>').text(oList.options[i].value).html());
        }
    }
    var result = "";
    for (j = 0; j < sdValues.length; j++) {
        result += sdValues[j] + "<br>";
    }
    if (result.length > 0) {
        result = result.substring(0, result.length - 4);
    }
    return result;
}

function removeLements(remElem){
    var strRemLem = remElem.split("|rem|");
    for (i = 0; i < strRemLem.length; i++) {
        if (strRemLem[i].length > 0) {
            var item1 = document.getElementById(strRemLem[i]);
            //item1.parentNode.removeChild(item1);
            item1.style.display = "none";
        }
    }
}

function createNewElement(val, id, width, height, isIE, addOverFlow){
    if (val == "") 
        val = "&nbsp;";
    var spanTag = document.createElement("span");
    spanTag.id = "span_" + id;
    spanTag.className = "controlFont";
    spanTag.innerHTML = "<span style='width:" + String(((addOverFlow == true && isIE == true) ? width - 4 : width)) + "px;display:inline;'>" + val +"</span>";
    var cssString = "";
    if (width != 0) {
        if (addOverFlow==true) cssString ="overflow:auto;";
        if (isIE) 
            cssString = (cssString=="" ? "overflow:hidden;" : cssString) + "width:" + String(width) + "px;white-space:normal;word-wrap:break-word;border:solid 1px #C0C0C0;min-height:" + String(height) + "px;margin:1px;padding:1px;display:inline-block; vertical-align: middle;";//background-color:#FFFFFF;
        else
            cssString = (cssString == "" ? "overflow:hidden;" : cssString) + "width:" + String(width) + "px;white-space:normal;border:solid 1px #C0C0C0;min-height:" + String(height) + "px;margin:1px;padding:1px;display:inline-block; vertical-align: middle;";
    }
    else {
        cssString = "margin:1px;" 
    }
    if (typeof(spanTag.style.cssText) == 'string') {
        spanTag.style.cssText = cssString;
    }
    spanTag.setAttribute('style', cssString);
    return spanTag;
}

function isIEBrowser()//by sanal on Dec 11
{
    var isOpera = false;
    var isIe = false;
    {
        var ua = navigator.userAgent;
        // Browser    
        isOpera = typeof opera != 'undefined';
        isIe = !isOpera && ua.indexOf('MSIE') != -1;
    }
    return isIe;
}
